<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="岗位分类" prop="identity"
                :rules="[
                 ]">
              <el-input v-model="inputForm.identity" placeholder="请填写岗位分类"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="绑定票面" prop="remarks"
                :rules="[
                 ]">
          <el-checkbox-group v-model="ticket">
           <el-checkbox-button v-for="item in tickets" :label="item" :key="item">{{item}}</el-checkbox-button>
         </el-checkbox-group>
           </el-form-item>
        </el-col> -->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          identity: '',
          oldIdentity: '',
          remarks: ''
        },
        ticket: [],
        tickets: [
        '所有中层干部(校领导票)',
        '教辅中层干部(服务对象票)',
        '教学教辅中层干部(同级票)',
        '机关中层干部(服务对象票)',
        '机关中层干部(同级票)',
        '中层干部(群众票)'
        ]
      }
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.ticket = []
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建岗位分类信息`
        } else if (method === 'edit') {
          this.title = '修改岗位分类信息'
        } else if (method === 'view') {
          this.title = '查看岗位分类信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/peopleinformation/identityiformation/peopleIdentityInformation/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.peopleIdentityInformation)
              this.inputForm.oldIdentity = this.inputForm.identity
              for (let i in this.inputForm.remarks.split(',')) {
               if (this.inputForm.remarks.split(',')[i] !== '') {
                 this.ticket.push(this.inputForm.remarks.split(',')[i])
               }
              }
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.inputForm.remarks = ''
        for (let i in this.ticket) {
          if (i === '0') {
            this.inputForm.remarks = this.ticket[i]
          } else {
            this.inputForm.remarks += ',' + this.ticket[i]
          }
        }
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/peopleinformation/identityiformation/peopleIdentityInformation/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
